package com.ujiuye.controller;

import com.ujiuye.model.Page;
import com.ujiuye.model.Student;
import com.ujiuye.service.StudentService;
import com.ujiuye.service.impl.StudentServiceImpl;
import com.ujiuye.util.DateUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.UUID;

/**
 * @MultipartConfig 标注该servlet要进行文件上传,如果不添加,获取的数据为null
 */
//@MultipartConfig
//@WebServlet("*.do")
public class StudentServlet extends BassServlet {

    public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
// 普通参数的获取
        String sname = req.getParameter("sname");
        String gender = req.getParameter("gender");
        String sbir = req.getParameter("sbir");
        String[] hobby = req.getParameterValues("hobby");

// 获取上传的文件
        Part part = req.getPart("file");
// 获取上传的文件名称
//String header = part.getHeader("Content-Disposition");
        String photos = part.getSubmittedFileName();
//System.out.println(photos + " = " + header);

// 获取上传文件的类型
        String suffix = photos.substring(photos.lastIndexOf("."));
// 为了解决文件名重复的问题,使用UUID重新生成文件名称
        String filename = UUID.randomUUID().toString() + suffix;
//System.out.println("filename = " + filename);
// 将图片保存到指定的磁盘
        String path = "E:\\idea\\code\\img";
        String absolutePath = path + File.separator + filename;
// System.out.println("absolutePath = " + absolutePath);
//将文件写入指定的磁盘,其中参数为磁盘的绝对路径
        part.write(absolutePath);
// 数据的封装
        Student student = new Student(sname, gender,
                DateUtils.strTransformerDate(sbir, "yyyy-MM-dd"), Arrays.toString(hobby), filename);
// 调用业务逻辑层方法
        StudentService ss = new StudentServiceImpl();
        boolean b = ss.saveStudent(student);
// 控制页面的跳转
        if (b) {
            resp.sendRedirect("http://localhost:8080/student_war_exploded/queryStu.do");
        } else {
            req.getRequestDispatcher("addStu.jsp").forward(req, resp);
        }
    }

    public void queryStu(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String currentPage = req.getParameter("currentPage");
        String sname = req.getParameter("sname");
        String gender = req.getParameter("gender");
        //2.数据封装
        Student student = new Student(sname, gender);
        // 3．调用业务逻辑层方法
        // 调用业务逻辑层方法
        StudentService ss = new StudentServiceImpl();
        Page<Student> page = ss.stuPageList(currentPage, student);
        // 4、页面跳转
        req.setAttribute("page", page);
        req.setAttribute("student", student);
        req.getRequestDispatcher("page/queryStu.jsp").forward(req, resp);
    }

    //根据学生编号批量删除学生信息
    public void deleteStudentById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        // 数据的获取
        String ids = req.getParameter("ids");
        // 调用业务逻辑层
        StudentService ss = new StudentServiceImpl();
        int num = ss.deleStudentByIds(ids);
        System.out.println("num =" + num);
        // 获取学生主页面的数据
        resp.sendRedirect("http://localhost:8080/student_war_exploded/queryStu.do");
    }

    //根据学生编号 获取学生信息
    public void updateById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String sid = req.getParameter("sid");
        StudentService ss = new StudentServiceImpl();
        Student student = ss.updateStudentByIds(sid);

        //将数据存放到作用域中
        req.setAttribute("student", student);
        req.getRequestDispatcher("page/updateStu.jsp").forward(req, resp);


    }

    public void updataStuDo(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String sid = req.getParameter("sid");
        String sname = req.getParameter("sname");
        String gender = req.getParameter("gender");
        String sbir = req.getParameter("sbir");
        String[] hobby = req.getParameterValues("hobby");
        String oldPhoto = req.getParameter("photo");
        Part file = req.getPart("file");
        // 获取上传文件的名称
        String photos = file.getSubmittedFileName();  // 如果没有文件上传 给方法返回空串
        // 如果不等于空串 表示需要进行文件上传
        if (!"".equals(photos)){
            String suffix = photos.substring(photos.lastIndexOf("."));
            // 如果有文件上传 就覆盖源文件的名称
            oldPhoto = UUID.randomUUID().toString() + suffix;
            // 将图片保存到指定的磁盘
            String path = "E:\\idea\\code\\img";
            String absolutePath = path + File.separator + oldPhoto;
            // System.out.println("absolutePath = " + absolutePath);
            //将文件写入指定的磁盘,其中参数为磁盘的绝对路径
            file.write(absolutePath);
        }

        // 学生数据的封装
        Student student = new Student(sname, gender,
                DateUtils.strTransformerDate(sbir, "yyyy-MM-dd"), Arrays.toString(hobby), oldPhoto);
        student.setSid(Integer.parseInt(sid));

        StudentService ss = new StudentServiceImpl();
        boolean bo =ss.updataStudentDo(student);
        //跳转到查询学生主页的方法 获取主页面数据  并跳转
        resp.sendRedirect("queryStu.do");

    }
}